/*
 * ================================================================================变量定义区
 */
var showCount = 10; //定义每页显示条数
var currentPage = 1;//全局变量，当前页
var page_index = 0;//页面索引
var chooseIds = new Array(); // 选定的部门标识数组

/*
 * ================================================================================页面初始化函数
 */
$(function()
{  
	//获取页面类型
	var pageType = $("#pageType").val();
	//获取上次列表
	if(pageType === 'lastest')
	{
		getLastestListAndForm();
	}
	else
	{
		getDataList1(currentPage);
	}

	//回车键绑定事件
	document.onkeydown = function(event)
	{
	    var e = event || window.event || arguments.callee.caller.arguments[0];
	     if(e && e.keyCode==13) // enter 键
	     {
	         //从第一页开始，查询数据
	    	 queryVdiSysInfoDataByName(1);
	     }
	};
});
/*
 * ================================================================================函数定义区
 * 
 */
/**
 * 获取列表数据
 * @param currentPage 请求的页码
 */
function getDataList1(pageIndex)
{
	var type = $("#type").val();
    var username = $("#username").val();
    
    SessionStorage.setSearchParam(
    		{
    		searchParam: 
    		{
    		showCount:showCount,
    		currentPage:pageIndex,
    		type:type,
    		username:username
    	}
    });
    chooseIds = new Array();
	$.ajax(
	{
		type: 'POST',
        url: BASE_PATH +'/vdiSystemInfo/getVdiSystemInfoList.do',
        dataType:"json",
        data:
        {
        	"showCount":showCount,
        	"currentPage":pageIndex,
        	"type":type,
        	"username":username,
        	"query_condition_field_list":"type,username"
        },
        cache:false,
        success:function(data,textStatus)
        {
        	var html = "";        	
        	var pageData = data;
        	var totalResult = pageData.queryParam.totalResult;
        	if(pageData.queryResult != null)
        	{
        		//解析data, 组装成html
	        	$.each(pageData.queryResult,function(i,n)
	        	{
	        		html += '<tr id="tr_index_' + i + '" '+ 'onclick="chooseRow(this,false);" '+'>'
        		
        			     + '<td title="' + switcHtml(n.name) + '">' + switcHtml(n.name) + '</td>';
        		
	        		html += '</td>'
		        		+ '<td style="width:220px;" title="' + switcHtml(n.remark) + '">' + switcHtml(n.remark) + '</td>'
		        		+ '<td style="width:220px;" name="vdi_id"  value=' + switcHtml(n.id) + ' hidden="hidden"'+'>' + switcHtml(n.id) + '</td>'
		        		+'<td style="text-align:left;" title="' + n.createUser + '">' + n.createUser + '</td>'	
		        		 + '<td style="text-align:center;" title="' + n.createTime + '">' + n.createTime + '</td>'
		        		 + '<td style="text-align:center;" title="';
        			
        			if(n.lastModifyTime == null)
        			{
        				html += "";
        			}
        			else 
        			{
        				html += n.lastModifyTime;
        			}
        			html += '">'+n.lastModifyTime;
	        		html += '</td>'+'<td style="text-align:left;" title="' + n.lastModifyUser + '">' + n.lastModifyUser + '</td>'+'<td style="width: 1px;"></td></tr>';
	        	}); 
        	} 
        	else
        	{
        		html = '<tr><td colspan="2">No items.</td></tr>';
        	}
        	
        	$('#tBodyList').html(html);
        	currentPage = pageIndex;
    		if(totalResult == 0)
    		{
    			$('#tBodyList').html('<tr><td style="text-align:center;">No items.</td></tr>');
    			$("#pagination").hide();
    		} 
    		else 
    		{
    			// 创建分页
    			$("#pagination").show();
    			$("#pagination").pagination(totalResult, 
    			{
        			num_edge_entries: 1, //边缘页数
        			num_display_entries: 3, //主体页数
        			current_page:currentPage,
        			callback: getDataList1,
        			items_per_page:showCount //每页显示1项
    			});
    		};
    		chooseId= null;
        },
		error:function(e)
		{
			 openAlert("加载数据错误！"); 
			 return;
		}
	});
} 

/**
 * 格式化日期
 * @param date
 * @returns {String}
 */
function formatDate(date)
{
	var thisDate = new Date();
	thisDate.setTime(date);
	var month = parseInt(thisDate.getMonth()+1) + "";
	var day = thisDate.getDate()+ "";
	var monthStr;
	var dayStr;
	
	if ( month.length == 1)
	{
		monthStr = '0' + month;
	} 
	else 
	{
		monthStr = month;
	}

	if(day.length == 1)
	{
		dayStr = '0' + day;
	} 
	else 
	{
		dayStr = day;
	}
	
	return thisDate.getFullYear()+"-"+monthStr+"-"+dayStr;
}

/**
 * 转换成日期格式
 * @param date
 * @returns {Date}
 */
function changeTime(date) 
{
	return new Date(date);
}

/**
 * 新建VDI系统信息
 */
function createVdiSysInfo()
{
	$("#submitForm").submit();
}

/**
 * 
* 修改按钮事件
*/
function editVdiSysInfo()
{
	if (chooseIds.length==1) 
	{
		var id = getSelectedRows();
		var vdiid = $("#"+id).find("td[name='vdi_id']").attr("value");
		$("#submitUpdateVdiForm").find("input[name='id']").val(vdiid);
		$("#submitUpdateVdiForm").submit();
		chooseIds = new Array();
	}
	else if(chooseIds.length>1)
	{
		openAlert("最多选择一条记录！"); 
	}else
	{
		openAlert("至少选择一条记录！"); 
	}
}

/**
* 删除按钮事件
*/
function deleteVdiSysInfo()
{
	if (chooseIds.length > 0) 
	{
		var ids = new Array();
		
		for( var i = 0; i < chooseIds.length; i++ )
		{
			var id = $("#" + chooseIds[i] ).children("td").eq(2).text();
			// 拼接需要移除的VDI集合
			ids.push( encodeURIComponent(id) );
		}
		
		var tempar = new Array();
		tempar.push(1);
		openConfirm("确认删除吗？", this, function()
		{
			$.ajax(
			{
				type: 'POST',
				url : BASE_PATH + "/vdiSystemInfo/deleteVdiSysInfo.do?ids=" + encodeURIComponent(ids),
		        dataType:"json",
		        data:
		        {
		        },
				success : function( data ) 
				{
					if( data == 'success' )
					{
						openAlert('删除成功',function()
						{
							chooseIds = new Array();
							getDataList1(currentPage);
						}
						);
					} 
					else
					{
						openAlert('删除失败');
					}
				}
			}
			);
		}
		, tempar);
	} 
	else 
	{
		openAlert("请至少选择一条记录！"); 
	}
}

/**
 * 根据查询条件调用查询函数
 */
function queryDataVdi()
{
	chooseId = null;
	queryVdiSysInfoDataByName(1);
}

/**
 * 根据查询条件查询列表
 * @param pageIndex 
 */
function queryVdiSysInfoDataByName(pageIndex)
{
	 var username = $("#usernamepermisson").val();
	 var vdiSysName = $("#vdiSysName").val().toUpperCase();
	 chooseIds = new Array();
	 
	 $.ajax(
		{
			type: 'POST',
	        url: BASE_PATH +'/vdiSystemInfo/queryVdiSysInfoDataByName.do',
	        dataType:"json",
	        data:
	        {
	        	"showCount":showCount,
	        	"currentPage":pageIndex,
	        	"vdiName":vdiSysName,
	        	"username":username,
	        	"query_condition_field_list":"vdiName,username"
	        },
	        cache:false,
	        success:function(data,textStatus)
	        {
	        	var html = "";        	
	        	var pageData = data;
	        	var totalResult = pageData.queryParam.totalResult;
	        	if(pageData.queryResult != null && totalResult>0)
	        	{
	        		//解析data, 组装成html
		        	$.each(pageData.queryResult,function(i,n)
		        	{
		        		html += '<tr id="tr_index_' + i + '" '+ 'onclick="chooseRow(this,false);" '+'>'
	        		
	        			     + '<td title="' + switcHtml(n.name) + '">' + switcHtml(n.name) + '</td>';
	        		
		        		html += '</td>'
			        		+ '<td style="width:220px;" title=' + switcHtml(n.remark) + '>' + switcHtml(n.remark) + '</td>'
			        		+ '<td style="width:220px;" name="vdi_id"  value=' + switcHtml(n.id) + ' hidden="hidden"'+'>' + switcHtml(n.id) + '</td>'
			        		+'<td style="text-align:center;" title=' + n.createUser + '>' + n.createUser + '</td>'	
			        		 + '<td style="text-align:center;" title=' + n.createTime + '>' + n.createTime + '</td>'
			        		 + '<td style="text-align:center;" title=';
	        			
	        			if(n.lastModifyTime == null)
	        			{
	        				html += "";
	        			}
	        			else 
	        			{
	        				html += n.lastModifyTime;
	        			}
	        			html += '>'+n.lastModifyTime;
		        		html += '</td>'+'<td style="text-align:center;" title=' + n.lastModifyUser + '>' + n.lastModifyUser + '</td>'+'<td style="width: 1px;"></td></tr>';
		        	}); 
	        	} 
	        	else
	        	{
	        		html = '<tr><td colspan="2" style="text-align:center;">No items.</td></tr>';
	        		$("#pagination").hide();
	        	}
	        	
	        	$('#tBodyList').html(html);
	        	currentPage = pageIndex;
	    		if(totalResult == 0)
	    		{
	    			$('#tBodyList').html('<tr><td style="text-align:center;">No items.</td></tr>');
	    			$("#pagination").hide();
	    		} 
	    		else 
	    		{
	    			// 创建分页
	    			$("#pagination").show();
	    			$("#pagination").pagination(totalResult, 
	    			{
	    				num_edge_entries: 1, //边缘页数
	    				num_display_entries: 3, //主体页数
	    				current_page:currentPage,
	    				callback: queryVdiSysInfoDataByName,
	    				items_per_page:showCount //每页显示1项
	    			});
	    		}
	    		
	    		
	        },
			error:function(e)
			{
				 openAlert("加载数据错误！"); 
				 return;
			}
		});
}

/**
 * 获取最近一次的列表
 * 从SessionStorage中获取参数
 */
function getLastestListAndForm()
{
	//获取参数
	var param = SessionStorage.getSearchParam(),
	//搜索参数
	searchParam = param.searchParam;

	getDataList1(searchParam.currentPage);
	//向SessionStorage存储数据
	SessionStorage.setSearchParam({
		searchParam: searchParam
	});
}

/**
 * 清空查询条件
 */
function resetInputButton()
{
	$("#vdiSysName").val("");
}

/**
 * 选择一行
 */
function chooseRow(o) 
{
	var id = $(o).attr("id");
	var flag = false;
	var index = null;
	if (chooseIds.length > 0) 
	{
		for (var i = 0; i < chooseIds.length; i++) 
		{
			if (chooseIds[i] == id) 
			{
				index = i;
				flag = true;
				break;
			}
		}
	}
	
	if (flag) 
	{
		$(o).removeClass("list_choosedRow");
		chooseIds = deleteArrayByIndex(index, chooseIds);
	} 
	else 
	{
		$(o).addClass("list_choosedRow");
		chooseIds.push(id);
	}
}